﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using BDDfyIt.Core;

namespace BDDfyIt.ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string storyFile, outputFile, outputNamespace, outputClassName;
                bool isHelp;
                ReadArguments(args, out storyFile, out outputFile, out outputNamespace, out outputClassName, out isHelp);
                if (isHelp)
                {
                    Console.Write(File.ReadAllText("ReadMe.txt"));
                    return;
                }
                IBDDfyItCodeGenerator<TextStory> codeGenerator = new TextStoryBDDfyItCodeGenerator();
                var storyModel = TextStoryParser.Parse(storyFile);
                codeGenerator.BDDfyIt(storyModel, outputFile, outputNamespace, outputClassName);
                Console.Write("Successfully parsed your story. Please check the output file for details");
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }
        }

        private static void ReadArguments(IList<string> args, out string storyFile, out string outputFile, out string outputNamespace, out string outputClassName, out bool isHelp)
        {
            storyFile = string.Empty;
            outputFile = string.Empty;
            outputNamespace = string.Empty;
            outputClassName = string.Empty;
            isHelp = false;
            if (args == null || args.Count <= 0)
            {
                return;
            }
            if (args.Contains(":help"))
            {
                return;
            }
            storyFile = args[0];
            if (args.Contains(":o"))
            {
                var index = args.IndexOf(":o") + 1;
                if (index < args.Count && string.IsNullOrEmpty(args[index]) == false)
                {
                    outputFile = args[index];
                }
            }
            if (args.Contains(":n"))
            {
                var index = args.IndexOf(":n") + 1;
                if (index < args.Count && string.IsNullOrEmpty(args[index]) == false)
                {
                    outputNamespace = args[index];
                }
            }
            if (args.Contains(":c"))
            {
                var index = args.IndexOf(":c") + 1;
                if (index < args.Count && string.IsNullOrEmpty(args[index]) == false)
                {
                    outputClassName = args[index];
                }
            }
        }
    }
}
